export default function throttle(fn, interval, immediate = true) {
  let startTime = 0

  const _throttle = function (...args) {
    return new Promise((resolve, reject) => {
      try {
        const nowTime = new Date().getTime()

        if (!immediate && startTime === 0) {
          startTime = nowTime
        }

        const waitTime = interval - (nowTime - startTime)
        if (waitTime <= 0) {
          const res = fn.apply(this, args)
          resolve(res)
          startTime = nowTime
        }
      } catch (error) {
        reject(new Error(error))
      }
    })
  }
  return _throttle
}